<template>
  <Form ref="otpForm" :model="form" :rules="rules" @keydown.enter.native="handleSubmit2">
    <FormItem prop="phoneNumber">
      <Input v-model="form.phoneNumber" placeholder="请输入手机号">
        <span slot="prepend">
          <Icon :size="16" type="ios-person"></Icon>
        </span>
      </Input>
    </FormItem>
    <FormItem prop="otp" style="display: inline-block;">
      <Input type="" v-model="form.otp" placeholder="请输入验证码" style="width: 150px;">
        <span slot="prepend">
          <Icon :size="14" type="md-home"></Icon>
        </span>
      </Input>
    </FormItem>
    <div v-if="!sendMsgDisabled" @click="count" style="margin-bottom: 10px;" class="niceButton">获取验证码</div>
    <div v-if="sendMsgDisabled" class="niceButton">{{ time+'秒后获取' }}</div>

    <FormItem>
      <Button @click="handleSubmit2" type="primary" long>登录</Button>
    </FormItem>
  </Form>
</template>
<script>
import {
  getOtp
} from '@/api/user'

export default {
  name: 'OtpForm',
  props: {
    phoneRules: {
      type: Array,
      default: () => {
        return [
          { required: true, message: '账号不能为空', trigger: 'blur' }
        ]
      }
    },
    otpRules: {
      type: Array,
      default: () => {
        return [
          { required: true, message: '验证码不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  data () {
    return {
      form: {
        phoneNumber: '',
        otp: ''
      },
      time: 60, // 发送验证码倒计时
      sendMsgDisabled: false
    }
  },
  computed: {
    rules () {
      return {
        phoneNumber: this.phoneRules,
        password: this.otpRules
      }
    }
  },
  methods: {
    handleSubmit2 () {
      this.$refs.otpForm.validate((valid) => {
        if (valid) {
          this.$emit('on-success-valid2', {
            phone_number: this.form.phoneNumber,
            otp: this.form.otp
          })
        }
      })
    },
    toRegister(){
      this.pwdLogin = false
      this.dxLogin = false
      this.register = true
    },
    toDx(){
        this.pwdLogin = false
        this.register = false
        this.dxLogin = true
        
    },
    toLogin(){
        this.register = false
        this.dxLogin = false
        this.pwdLogin = true
    },
    count(){
      const that = this
      let phoneNumber = that.form.phoneNumber
      console.log(phoneNumber)
      getOtp({phoneNumber })
        that.sendMsgDisabled = true
        const interval = window.setInterval(function() {
          if ((that.time--) <= 0) {
            that.time = 60
            that.sendMsgDisabled = false
            window.clearInterval(interval)
          }
        }, 1000)
    }
  }
}
</script>

<style>
 .niceButton {
              align-items: center;
              background: #FFFFFF;
              border: 0 solid #E2E8F0;
              box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
              box-sizing: border-box;
              color: #1A202C;
              display: inline-flex;
              font-family: Inter, sans-serif;
              font-size: 0.8rem;
              /* font-weight: 700; */
              height: 30px;
              justify-content: center;
              line-height: 16px;
              overflow-wrap: break-word;
              /* padding: 24px; */
              text-decoration: none;
              width: auto;
              /* border-radius: 8px; */
              cursor: pointer;
              user-select: none;
              -webkit-user-select: none;
              touch-action: manipulation;
            }

</style>
